package org.example;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * A Camel Application
 */
public class MainApp {

    /**
     * A main() so we can easily run these routing rules in our IDE
     */
    public static void main(String... args) throws Exception {
//        Main main = new Main();
//        main.configure().addRoutesBuilder(new MyRouteBuilder());
//        main.run(args);

        MongoClient mongoClient = new MongoClient("127.0.0.1",27030);
        MongoDatabase database = mongoClient.getDatabase("test");
        MongoCollection<Document> person = database.getCollection("person");

        FindIterable<Document> documents = person.find();
        for(Document document : documents){
            System.out.println(document.toJson());
        }


    }

    MongoClient mongoClient;
    MongoDatabase database;
    @Before
    public void testBefore(){
        mongoClient = new MongoClient("127.0.0.1",27030);
        database = mongoClient.getDatabase("test");
    }

    @After
    public void testAfter(){
        mongoClient.close();
    }

    @Test
    public void testInsert(){
        long startTime = System.currentTimeMillis();
        for(int i = 0 ; i < 10000; i++){
            Document document = new Document()
                .append("name","name_"+i)
                .append("age",i);
            MongoCollection<Document> person = database.getCollection("person");
            person.insertOne(document);
        }
        System.out.println("耗时："+(System.currentTimeMillis()-startTime));
    }

    public void testDelete(){}

    public void testUpdate(){}

    @Test
    public void testFind(){
        long startTime = System.currentTimeMillis();
        MongoCollection<Document> person = database.getCollection("person");
        FindIterable<Document> documents = person.find();
        for(Document document : documents){
            System.out.println(document.toJson());
        }
        System.out.println("耗时："+(System.currentTimeMillis()-startTime));
    }

}

